package splitencrypt

// 主要加密文件 将流加密
import (
	"crypto/rand"
	"crypto/rsa"
	"crypto/x509"
	"encoding/pem"
	"errors"
)

// // 将流使用RSA私钥加密
// func encryptStream(privateKey []byte) ([]byte, error) {
// 	var r []byte
// 	fmt.Printf("encrypt Stream!")
// 	return r, nil
// }

// // 将字符串私钥转化为字节流私钥
// func convertKeyToByte(privateKey string) ([]byte, error) {
// 	var r []byte
// 	fmt.Printf("convert Key To Byte!")
// 	return r, nil
// }

// 公钥和私钥可以从文件中读取
var privateKey = []byte(`
-----BEGIN RSA PRIVATE KEY-----
MIIJKQIBAAKCAgEA0xHzW2BHgByFBx3RMIlD8vVCpiFTPxr77XBaOl2p+34rXoBr
tKuEaytkbRqL2Cds6SCSut2RcAzWQ1QWA2jElIo+KErE2uZcLNj2U/HWgikHH15X
4lFFpG+HFrJdyrhbzJDidqiqkcjxgti8sYRm9JsPPR/NpToskmeLFsKsd+gogimm
mZrwelYXU2Jze70I/v+3xeeC5k8b8RQWbe7aWhZsp04amz9NSRXUgoVwxUcW9j10
IYLXcVlrnmyl1Lx6grEwKVFSFRkx3/EElyh+noQPe1mAuXlk21qylowhRmAW5KaY
jmlv4FDqho0ytXGeUj8ikTkBAkZa9ZVJrYm7DyBYKCub1ND8419wQEsv4fh1dqA+
tw+R+6ibsE3HEEuE0cM3sP/UmO9zvlY8LMabzKAmCweBtt8VxTfZlRiudxQzF2mo
QUrn4Ztdco6zXgB03+/MYvSfDpmzTCpPiS1Uef9yJNNzSRrc/G41Qz1XSkt4juvh
cyKRHvjTjkSxx33i3oEVYzu423n4g/PNXeRhUUst4A2Sj7575TK8Ev2RXxK9m2mX
FhaA9N1O0txrL2BxYOWgGPxUmn24ocDix/1JNweaQFkAXsCDmdRKMXUVErAQNAiD
s8Lw3miqZmGXcaVu6OkKBXjIQrGgtZ76pBOv36dCnnBmThIxy5UJOveQl6UCAwEA
AQKCAgEApyj9Qa3jkouDs1oi9TyQytcAvM+BvWWiha5IVxcc6ydGM6aj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fkGYeJYxR0TXJaiQBbZYHAxodPHi4ivrA4lXbOBmYyfxsXbVDGNNfCVSfnzhI
8zDspGkXhFXJFPdSQXRoqzxhRU6F0/w0CgAszRl+G4RzIyP1+uT/91kAZvnb0e+N
iQKhXfw7EickkDwlAZz14WLcFXMK1bSSpTh2fmEujTep4yGu9AM2P37CSw4sl72j
k1wnJHFR37SX2O9wSk3ZS6SLQYlmhtlKpnTleExbmLx2jAc8Q2jfmwO6ghb6
-----END RSA PRIVATE KEY-----
`)

var publicKey = []byte(`
-----BEGIN PUBLIC KEY-----
MIICIjANBgkqhkiG9w0BAQEFAAOCAg8AMIICCgKCAgEA0xHzW2BHgByFBx3RMIlD
8vVCpiFTPxr77XBaOl2p+34rXoBrtKuEaytkbRqL2Cds6SCSut2RcAzWQ1QWA2jE
lIo+KErE2uZcLNj2U/HWgikHH15X4lFFpG+HFrJdyrhbzJDidqiqkcjxgti8sYRm
9JsPPR/NpToskmeLFsKsd+gogimmmZrwelYXU2Jze70I/v+3xeeC5k8b8RQWbe7a
WhZsp04amz9NSRXUgoVwxUcW9j10IYLXcVlrnmyl1Lx6grEwKVFSFRkx3/EElyh+
noQPe1mAuXlk21qylowhRmAW5KaYjmlv4FDqho0ytXGeUj8ikTkBAkZa9ZVJrYm7
DyBYKCub1ND8419wQEsv4fh1dqA+tw+R+6ibsE3HEEuE0cM3sP/UmO9zvlY8LMab
zKAmCweBtt8VxTfZlRiudxQzF2moQUrn4Ztdco6zXgB03+/MYvSfDpmzTCpPiS1U
ef9yJNNzSRrc/G41Qz1XSkt4juvhcyKRHvjTjkSxx33i3oEVYzu423n4g/PNXeRh
UUst4A2Sj7575TK8Ev2RXxK9m2mXFhaA9N1O0txrL2BxYOWgGPxUmn24ocDix/1J
NweaQFkAXsCDmdRKMXUVErAQNAiDs8Lw3miqZmGXcaVu6OkKBXjIQrGgtZ76pBOv
36dCnnBmThIxy5UJOveQl6UCAwEAAQ==
-----END PUBLIC KEY-----
`)

// 加密
func RsaEncrypt(origData []byte) ([]byte, error) {
	block, _ := pem.Decode(publicKey)
	if block == nil {
		return nil, errors.New("public key error")
	}
	pubInterface, err := x509.ParsePKIXPublicKey(block.Bytes)
	if err != nil {
		return nil, err
	}
	pub := pubInterface.(*rsa.PublicKey)
	return rsa.EncryptPKCS1v15(rand.Reader, pub, origData)
}

// 解密
func RsaDecrypt(ciphertext []byte) ([]byte, error) {
	block, _ := pem.Decode(privateKey)
	if block == nil {
		return nil, errors.New("private key error!")
	}
	priv, err := x509.ParsePKCS1PrivateKey(block.Bytes)
	if err != nil {
		return nil, err
	}
	return rsa.DecryptPKCS1v15(rand.Reader, priv, ciphertext)
}
